﻿using B.S.BaseData.ErrorCode;
using B.S.BaseData.ErrorCode.ErrorCodes;
using B.S.DataBase.EFCoreServiers.Dtos.Depts;
using B.S.DataBase.EFCoreServiers.Dtos.Roles;
using B.S.DataBase.EFCoreServiers.IServices.Dtpes;
using B.S.DataBase.EFCoreServiers.IServices.Roles;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace B.S.BaseData.ReadAPI.Controllers
{
    /// <summary>
    /// 部门管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    [ApiExplorerSettings(GroupName = "Depts")]
    // 如果只想给本控制器加日志过滤器，直接加特性（如需全局注册可移除此行）
    // [LogOperationAttribute("部门管理操作")]
    public class DepratmentController : ControllerBase
    {
        private readonly IDepartmentServices departmentServices;
        private readonly ILogger<DepratmentController> _logger;

        public DepratmentController(IDepartmentServices departmentServices, ILogger<DepratmentController> logger)
        {
            this.departmentServices = departmentServices;
            _logger = logger;
        }

        /// <summary>
        /// 部门列表
        /// </summary>
        /// <param name="departmentDto"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<ApiPaging<GetDepartmentResponseDto>>> GetDepartmentListAsync([FromQuery]GetDepartmentDto departmentDto)
        {
            try
            {
                _logger.LogInformation($"部门列表显示成功");
                return await departmentServices.GetDepartmentListAsync(departmentDto);
            }
            catch (System.Exception ex)
            {
                _logger.LogError(ex, $"部门列表显示发生异常");
                return ApiResult<ApiPaging<GetDepartmentResponseDto>>.Fail("角部门列表显示发生异常，请稍后重试", StatusCodeEnum.Exception);
            }
        }
        /// <summary>
        /// 获取部门树形结构
        /// </summary>
        /// <param name="ParentId"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<IList<DeptItemTree>>> GetDeptTree(int ParentId = 0)
        {
            try
            {
                var res = await departmentServices.GetDeptTree(ParentId);
                if (res != null)
                {
                    _logger.LogInformation("获取部门树形数据集: ", ParentId);
                }
                return res;
            }
            catch (Exception ex)
            {
                return ApiResult<IList<DeptItemTree>>.Fail("获取部门树形数据集失败：" + ex.Message, StatusCodeEnum.Exception);
            }
        }
        /// <summary>
        /// 获取用户部门树形结构，按照一级部门组织用户和角色信息
        /// </summary>
        /// <param name="ParentId">父部门ID，默认为0表示顶级部门</param>
        /// <returns>返回树形结构的部门-角色-用户数据</returns>
        [HttpGet]
        public async Task<ApiResult<List<UserDeptItemTree>>> GetUserDeptTree(int ParentId = 0)
        {
            try
            {
                var res = await departmentServices.GetUserDeptTree(ParentId);
                if (res != null)
                {
                    _logger.LogInformation("开始获取用户部门树形结构: ", ParentId);
                }
                return res;
            }
            catch (Exception ex)
            {
                return ApiResult<List<UserDeptItemTree>>.Fail("获取用户部门树形结构失败：" + ex.Message, StatusCodeEnum.Exception);
            }
        }

    }
}
